package com.ntschy.web.controller.bt;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ntschy.bt.entity.dataobject.BtSupplyShop;
import com.ntschy.bt.entity.dto.BtSupplyShopAddUpdateDTO;
import com.ntschy.bt.entity.dto.BtSupplyShopPageDTO;
import com.ntschy.bt.entity.mapstruct.BtSupplyShopMapstruct;
import com.ntschy.bt.entity.valid.BtSupplyShopGroups;
import com.ntschy.bt.service.IBtSupplyShopService;
import com.ntschy.common.core.AjaxPageResult;
import com.ntschy.common.core.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.hibernate.validator.constraints.Length;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.ntschy.common.core.controller.BaseController;

import javax.validation.constraints.NotBlank;
import java.util.List;

/**
 * <p>
 * 供需超市 前端控制器
 * </p>
 *
 * @author cm
 * @since 2020-12-28
 */
@RestController
@RequestMapping("/bt/supplyShop")
@Api(tags = "供需超市接口")
@Validated
public class BtSupplyShopController extends BaseController {

    @Autowired
    private IBtSupplyShopService supplyShopService;


    // * ================================================基础增删改查BEGIN============================================= *
    /**
     * 查询供需超市列表
     *
     * @param pageDTO
     * @return
     */
    @ApiOperation(value = "查询供需超市列表", notes = "")
    @ApiImplicitParam(name = "queryDto", value = "供需超市列表查询实体类", required = true, dataType = "BtSupplyShopPageDTO")
    //@PreAuthorize("@ss.hasPermi('bt:supplyshop:page')")
    @PostMapping("/pageSupplyShop")
    public AjaxPageResult<BtSupplyShop> pageSupplyShop(@RequestBody BtSupplyShopPageDTO pageDTO) {
        Page<BtSupplyShop> page = PageHelper.startPage(pageDTO.getCurrPage(), pageDTO.getPageSize());
        BtSupplyShop querySupplyShop = BtSupplyShopMapstruct.BT_SUPPLY_SHOP_MAP_STRUCT.pageDtoToDo(pageDTO);
        List<BtSupplyShop> list = supplyShopService.pageSupplyShop(querySupplyShop);
        return buildPageResult(list, page);
    }

    /**
     * 新增供需超市
     *
     * @param addDTO
     * @return
     */
    @ApiOperation(value = "新增供需超市", notes = "")
    @ApiImplicitParam(name = "addDTO", value = "新增供需超市实体类", required = true, dataType = "BtSupplyShopPageDTO")
    //@PreAuthorize("@ss.hasPermi('bt:supplyshop:add')")
    @PostMapping("/addSupplyShop")
    public AjaxResult<String> addSupplyShop(@Validated(BtSupplyShopGroups.Add.class) @RequestBody BtSupplyShopAddUpdateDTO addDTO) {
        BtSupplyShop addSupplyShop = BtSupplyShopMapstruct.BT_SUPPLY_SHOP_MAP_STRUCT.addUpdateDtoToDo(addDTO);
        return toAjax(supplyShopService.addSupplyShop(addSupplyShop), "新增成功", "新增失败");
    }

    /**
     * 更新供需超市
     *
     * @param updateDTO
     * @return
     */
    @ApiOperation(value = "更新供需超市", notes = "")
    @ApiImplicitParam(name = "updateDTO", value = "更新供需超市实体类", required = true, dataType = "BtSupplyShopPageDTO")
    //@PreAuthorize("@ss.hasPermi('bt:supplyshop:update')")
    @PostMapping("/updateSupplyShop")
    public AjaxResult<String> updateSupplyShop(@Validated(BtSupplyShopGroups.Update.class) @RequestBody BtSupplyShopAddUpdateDTO updateDTO) {
        BtSupplyShop updateSupplyShop = BtSupplyShopMapstruct.BT_SUPPLY_SHOP_MAP_STRUCT.addUpdateDtoToDo(updateDTO);
        return toAjax(supplyShopService.updateSupplyShop(updateSupplyShop), "更新成功", "更新失败");
    }

    /**
     * 删除供需超市
     *
     * @param supplyShopId
     * @return
     */
    @ApiOperation(value = "删除供需超市", notes = "")
    @ApiImplicitParam(name = "userId", value = "供需超市ID", required = true, dataType = "String")
    //@PreAuthorize("@ss.hasPermi('bt:supplyshop:delete')")
    @GetMapping("/deleteSupplyShop")
    public AjaxResult<String> deleteSupplyShop(@NotBlank(message = "供需超市ID不能为空") @Length(min = 36, max = 36, message = "供需超市ID长度不合法") String supplyShopId) {
        return toAjax(supplyShopService.deleteSupplyShop(supplyShopId), "删除成功", "删除失败");
    }
    // * =================================================基础增删改查END============================================== *

}
